<template>
  <div class="detail-inner">
    <el-card>
      <i @click="back" class="el-icon-arrow-left liu"></i>
      <el-tabs v-model="activeName">
        <el-tab-pane label="登录账户设置">
          <el-form
            ref="liu"
            :model="formUser"
            label-width="120px"
            :rules="rules"
            style="margin-left: 120px; margin-top: 30px"
          >
            <el-form-item label="姓名" prop="username">
              <el-input
                v-model="formUser.username"
                style="width: 300px"
              ></el-input>
            </el-form-item>
            <el-form-item label="密码" prop="password2">
              <el-input
                v-model="formUser.password2"
                style="width: 300px"
              ></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="onSubmit">保存</el-button>
            </el-form-item>
          </el-form>
        </el-tab-pane>
        <el-tab-pane label="个人详情">
          <el-row type="flex" justify="end">
            <el-tooltip content="打印个人基本信息">
              <router-link :to="`/print/${userId}?type=personal`">
                <i class="el-icon-printer" />
              </router-link>
            </el-tooltip>
          </el-row>
          <component :is="userComponent" />
        </el-tab-pane>
        <el-tab-pane label="岗位信息">
          <el-row type="flex" justify="end">
            <el-tooltip content="打印岗位信息">
              <router-link :to="`/print/${userId}?type=job`">
                <i class="el-icon-printer" />
              </router-link>
            </el-tooltip>
          </el-row>
          <component :is="JobInfo" />
        </el-tab-pane>
      </el-tabs>
    </el-card>
  </div>
</template>

<script>
import { getUserPhoto } from "@/api/user";
import { saveUserDetailById } from "@/api/employees";
import UserInfo from "./user-info.vue";
import JobInfo from "./job-info.vue";
export default {
  created() {
    this.getList();
    console.log();
  },
  data() {
    return {
      activeName: 0,
      userComponent: "user-info",
      JobInfo: "job-info",
      formUser: {
        username: "",
        password2: "",
      },
      userId: this.$route.params.id,
      rules: {
        username: [
          { required: true, message: "姓名不能为空", trigger: "blur" },
        ],
        password2: [
          { required: true, message: "密码不能为空", trigger: "blur" },
          { min: 6, max: 9, message: "密码长度6-9位", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    async getList() {
      const data = await getUserPhoto(this.$route.params.id);
      this.formUser = data;
    },
    async onSubmit() {
      try {
        await this.$refs.liu.validate();
        await saveUserDetailById({
          ...this.formUser,
          password: this.formUser.password2,
        });
        this.$message({
          message: "修改成功",
          type: "success",
        });
      } catch (error) {
        console.log(error);
      }
    },
    back() {
      this.$router.back();
    },
  },
  components: {
    UserInfo,
    JobInfo,
  },
};
</script>

<style lang="sass" scoped>
.detail-inner
  padding: 20px

.liu
  font-size: 20px
  cursor: pointer
</style>
